import {createRouter, createWebHistory} from 'vue-router'
import {tokenStore} from "@/stores/token.js";
import HomeView from "@/views/admin/HomeView.vue";
import CategoryView from "@/views/admin/CategoryView.vue";
import GoodsView from "@/views/admin/GoodsView.vue";
import IndexView from "@/views/admin/IndexView.vue";
import UserView from "@/views/admin/UserView.vue";
import OrderView from "@/views/admin/OrderView.vue";
import AdminView from "@/views/admin/AdminView.vue";
import LoginView from "@/views/admin/LoginView.vue";
import UserLoginView from "@/views/user/UserLoginView.vue";
import FrontHomeView from "@/views/user/FrontHomeView.vue";
import UserTestView from "@/views/user/UserTestView.vue";
import UserIndexView from "@/views/user/UserIndexView.vue";
import UserSearchView from "@/views/user/UserSearchView.vue";
import UserRegView from "@/views/user/UserRegView.vue";
import UserGoodsView from "@/views/user/UserGoodsView.vue";
import UserCartView from "@/views/user/UserCartView.vue";
import UserCreateOrderView from "@/views/user/UserCreateOrderView.vue";
import UserCenterView from "@/views/user/UserCenterView.vue";
import CenterAddrListView from "@/views/user/center/CenterAddrListView.vue";
import CenterInfoView from "@/views/user/center/CenterInfoView.vue";
import CenterCollectListView from "@/views/user/center/CenterCollectListView.vue";
import CenterOrderListView from "@/views/user/center/CenterOrderListView.vue";
import CenterPasswordView from "@/views/user/center/CenterPasswordView.vue";
import CenterRechargeView from "@/views/user/center/CenterRechargeView.vue";
import UserPayView from "@/views/user/UserPayView.vue";
import CenterFeedbackView from "@/views/user/center/CenterFeedbackView.vue";


const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/admin',
            name: 'adminHome',
            component: HomeView,
            redirect: '/admin/index',
            children: [
                {path: 'category', component: CategoryView},
                {path: 'index', component: IndexView},
                {path: 'goods', component: GoodsView},
                {path: 'user', component: UserView},
                {path: 'order', component: OrderView},
                {path: 'admin', component: AdminView}
            ]
        },
        {
            path:'/admin/login',
            component:LoginView
        },
        {
            path:'/user/test',
            component:UserTestView
        },
        {
            path:'/',
            component:FrontHomeView,
            redirect: '/user/index',
            children:[
                {path:'/user/login',component:UserLoginView},
                {path: '/user/index',component: UserIndexView},
                {path: '/user/search/:categoryId?',component: UserSearchView,props:true},
                {path: '/user/reg',component: UserRegView},
                {path: '/user/goods',component: UserGoodsView},
                {path: '/user/cart',component: UserCartView},
                {path: '/user/order',component: UserCreateOrderView},
                {path: '/user/pay',component: UserPayView},
                {
                    path: '/user/center',
                    component: UserCenterView,
                    redirect: '/user/center/info',
                    children:[
                        {path:'/user/center/addr',component:CenterAddrListView},
                        {path:'/user/center/info',component:CenterInfoView},
                        {path:'/user/center/collect',component:CenterCollectListView},
                        {path:'/user/center/order',component:CenterOrderListView},
                        {path:'/user/center/pwd',component:CenterPasswordView},
                        {path:'/user/center/recharge',component:CenterRechargeView},
                        {path:'/user/center/feedback',component:CenterFeedbackView},
                    ]
                }
            ]
        }
    ]
})

// 配置导航守卫
router.beforeEach((to, from) => {
//     to-跳转到的位置
//     from-原位置
//     todo 将来还会对其他页面放行，例如前台首页，搜索页、详情页、用户登录页、注册页
    let reg = /^\/user\/search.*/
    if (to.path === '/admin/login' || to.path === '/user/login' || to.path === '/user/index' || to.path === '/user/reg'
        || reg.test(to.path) || to.path === '/' || to.path === '/user/goods'
    ) {
        return true
    } else {
    //     先去判断是否有token，有放行，没用去登录页
        const token = tokenStore();
        if (token.tokenStr) {
            return true
        } else {
            // 判断跳转到管理员登录页还是跳转到用户登录页
            // let path = router.currentRoute.value.path;
            console.log(router.currentRoute.value);
            let reg = /^\/admin.*/
            if (reg.test(to.path)) {
                return '/admin/login'
            } else {
                return '/user/login'
            }

        }
    }
})

export default router
